package com.dialogutils;

import android.content.Context;
import android.view.*;
import android.widget.*;
import com.tools.R;

/**
 * Created by weishj on 2018/1/11.
 */
public class DialogUtils extends BaseDialog implements View.OnClickListener {
    private TextView title;
    private ImageView close;
    private EditText msgEdit;
    private TextView confirm;
    private TextView cancel;
    private ButtonListener mBuIml;
    private String mDialogValue = "";

    public DialogUtils(Context context) {
        super(context);
    }

    @Override
    protected View loadView() {
        return LayoutInflater.from(context).inflate(R.layout.popup_dialog, null);
    }

    @Override
    protected void initView(View view) {
        this.title = view.findViewById(R.id.common_dialog_title_tv);
        this.close = view.findViewById(R.id.common_dialog_close_iv);
        this.msgEdit = view.findViewById(R.id.common_dialog_message_tv);
        this.confirm = view.findViewById(R.id.common_dialog_confirm_tv);
        this.cancel = view.findViewById(R.id.common_dialog_cancel_tv);

        this.confirm.setText("确定");
        this.cancel.setText("取消");
        this.close.setOnClickListener(this);
        this.confirm.setOnClickListener(this);
        this.cancel.setOnClickListener(this);
    }

    public void setDialogTitle(CharSequence title) {
        this.title.setText(title);
    }

    public void setCloseBuShow(boolean closeBtn) {
        if (closeBtn) {
            this.close.setVisibility(View.VISIBLE);
        } else {
            this.close.setVisibility(View.GONE);
        }
    }

    public void setDialogHint(CharSequence msg) {
        this.msgEdit.setHint(msg);
    }

    public void setDialogValue(String mDialogValue) {
        if (!mDialogValue.equals("") && !mDialogValue.equals("未知")) {
            this.msgEdit.setText(mDialogValue);
        }
    }

    /**
     * 简单对话框
     *
     * @param msg
     */
    public DialogUtils isMessageAlert(String msg) {
        this.title.setVisibility(View.GONE);
        this.msgEdit.setVisibility(View.VISIBLE);
        this.msgEdit.setText(msg);
        this.msgEdit.setFocusable(false);
        this.msgEdit.setEnabled(false);
        return this;
    }

    public DialogUtils setButtonIml(ButtonListener iml) {
        this.mBuIml = iml;
        return this;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.common_dialog_close_iv) {
            dismiss();
        } else if (id == R.id.common_dialog_confirm_tv) {
            if (!msgEdit.getText().toString().equals("")) {
                mBuIml.onAffirm(msgEdit.getText().toString());
                dismiss();
            } else {
                ToastUtil.show(context, "输入内容为空", Toast.LENGTH_SHORT);
            }
        } else if (id == R.id.common_dialog_cancel_tv) {
            dismiss();
            mBuIml.onCancel();
        }
    }

    public interface ButtonListener {
        void onCancel();

        void onAffirm(String text);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
}